﻿using System.Collections;
using System.IO;
using System.Linq;
using System.Modules.Extensions;
using System.Modules.SharePoint.Contracts.DTO;
using System.Xml.Linq;
using Microsoft.SharePoint;
using Microsoft.SharePoint.Administration;

namespace System.Modules.SharePoint.Extensions
{
    /// <summary>
    /// SP Element Definition Extensions
    /// </summary>
    public static class SPElementDefinitionExtensions
    {
        /// <summary>
        /// Update Files In Module
        /// </summary>
        /// <param name="definition"></param>
        /// <param name="web"></param>
        public static void UpdateFilesInModule(this SPElementDefinition definition, SPWeb web)
        {
            XElement xml = definition.XmlDefinition.ToXElement();
            XNamespace xmlns = "http://schemas.microsoft.com/sharepoint/";
            string featureDir = definition.FeatureDefinition.RootDirectory;
            SPFeatureModule module = (from m in xml.DescendantsAndSelf() select new SPFeatureModule {ProvisioningUrl = m.Attribute("Url").Value, PhysicalPath = featureDir, Files = (from f in m.Elements(xmlns.GetName("File")) select new SPModuleFile {Path = (f.Attribute("Path") == null) ? (m.Attribute("Path") == null) ? string.Empty : Path.Combine(featureDir, m.Attribute("Path").Value) : Path.Combine(featureDir, f.Attribute("Path").Value), Name = f.Attribute("Url").Value, Properties = (from p in f.Elements(xmlns.GetName("Property")) select p).ToDictionary(n => n.Attribute("Name").Value, v => v.Attribute("Value").Value)}).ToArray()}).First();

            if (module == null)
            {
                return;
            }

            foreach (SPModuleFile file in module.Files)
            {
                string physicalPath = string.IsNullOrEmpty(file.Path) ? Path.Combine(module.PhysicalPath, file.Name) : file.Path;
                string virtualPath = string.Concat(web.Url, "/", module.ProvisioningUrl, "/", file.Name);

                if (File.Exists(physicalPath))
                {
                    using (var sreader = new StreamReader(physicalPath))
                    {
                        SPFile spFile = web.GetFile(virtualPath);
                        if (spFile != null)
                        {
                            if (spFile.CheckOutType == SPFile.SPCheckOutType.None)
                            {
                                spFile.CheckOut();
                            }
                        }
                        spFile = web.Files.Add(virtualPath, sreader.BaseStream, new Hashtable(file.Properties), true);
                        spFile.Update();
                        if (spFile.CheckOutType != SPFile.SPCheckOutType.None)
                        {
                            spFile.CheckIn(String.Empty);
                        }
                        if (spFile.Item.ParentList.EnableMinorVersions)
                        {
                            spFile.Publish("Published automatically");
                        }
                        if (spFile.Item.ParentList.EnableModeration)
                        {
                            spFile.Approve("Approved Automatically");
                        }
                    }
                }
            }
        }
    }
}